package com.justerdu.mvpclient.login.view;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.justerdu.mvpclient.R;
import com.justerdu.mvpclient.login.presenter.IUserPresenter;
import com.justerdu.mvpclient.login.presenter.UserPresenter;
/**
 * @Author JusterDu 2016/11/19.
 * @Func User 登录Activity
 */
public class UserActivity extends ActionBarActivity implements IUserView, View.OnClickListener {

	private EditText editUsername;
	private EditText editPassword;
	private Button btnLogin;
	private Button btnClear;
	private ProgressBar progressBar;
	IUserPresenter userPresenter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 加载登录界面
		setContentView(R.layout.activity_login);

		editUsername = (EditText) this.findViewById(R.id.et_login_username);
		editPassword = (EditText) this.findViewById(R.id.et_login_password);
		btnLogin = (Button) this.findViewById(R.id.btn_login_login);
		btnClear = (Button) this.findViewById(R.id.btn_login_clear);
		progressBar = (ProgressBar) this.findViewById(R.id.progress_login);

		// 为Button设置监听
		btnLogin.setOnClickListener(this);
		btnClear.setOnClickListener(this);

		userPresenter = new UserPresenter(this);
		userPresenter.setProgressBarVisiblity(View.INVISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.btn_login_clear:
				userPresenter.clear();
				break;
			case R.id.btn_login_login:
				userPresenter.setProgressBarVisiblity(View.VISIBLE);
				btnLogin.setEnabled(false);
				btnClear.setEnabled(false);
				userPresenter.doLogin(editUsername.getText().toString(), editPassword.getText().toString());
				break;
		}
	}

	@Override
	public void onClearText() {
		editUsername.setText("");
		editPassword.setText("");
	}

	@Override
	public void onLoginResult(String result) {
		userPresenter.setProgressBarVisiblity(View.INVISIBLE);
		btnLogin.setEnabled(true);
		btnClear.setEnabled(true);
		if(result.equals("NETWORK_ERROR")){
			Toast.makeText(this,"NETWORK_ERROR···", Toast.LENGTH_SHORT).show();
		}else if (result.equals("SUCCESS")){
			Toast.makeText(this,"LOGIN_SUCCESS···", Toast.LENGTH_SHORT).show();
		}else
			Toast.makeText(this,"LOGIN_FAIL···", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onSetProgressBarVisibility(int visibility) {
		progressBar.setVisibility(visibility);
	}
	
}
